package leetcode

import kotlinetc.println


//https://leetcode.com/problems/valid-sudoku/
/**
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

 */
fun main(args: Array<String>) {
    'a'.toInt().println()
}

fun isValidSudoku(board: Array<CharArray>): Boolean {

    val row = board.size
    val col = board.first().size


    val rowTable = Array(row) {
        Array<Boolean>(col) { false }
    }
    val colTable = Array(row) {
        Array<Boolean>(col) { false }
    }

    val cellTable = Array(row) {
        Array<Boolean>(col) { false }
    }

    for (i in 0 until row)
        for (j in 0 until col) {
            val c = board[i][j]
            if (c !in '1'..'9')
                continue
            val numberIndex = board[i][j].toInt() - '1'.toInt()

            if (rowTable[i][numberIndex] || colTable[j][numberIndex] || cellTable[3 * (i / 3) + j / 3][numberIndex]) return false
            else {
                rowTable[i][numberIndex] = true
                colTable[j][numberIndex] = true
                cellTable[3 * (i / 3) + j / 3][numberIndex] = true
            }
        }

    return true
}